package com.qnw.CardGroupManagement.util;
import android.content.*;
import com.ygo.feihua.*;
import com.ygo.feihua.logo.*;
import java.util.*;
import android.util.*;

public class CardMessage
{

	
	static {
		
		CARD_RACE=getArray(R.array.CARD_RACE);
		CARD_RACE_16=getArray(R.array.CARD_RACE_16);
		CARD_EFFECT_TYPE=getArray(R.array.CARD_EFFECT_TYPE);
		CARD_EFFECT_TYPE_16=getArray(R.array.CARD_EFFECT_TYPE_16);
		CARD_RACE=getArray(R.array.CARD_RACE);

		CARD_OT=getArray(R.array.CARD_OT);
		CARD_OT_16=getArray(R.array.CARD_OT_16);
		CARD_LEVELS=getArray(R.array.CARD_LEVELS);
		CARD_LEVELS_16=getArray(R.array.CARD_LEVELS_16);
		CARD_TYPE=getArray(R.array.CARD_TYPE);
		CARD_TYPE_16=getArray(R.array.CARD_TYPE_16);

		CARD_ATTRIBUTE=getArray(R.array.CARD_ATTRIBUTE);
		CARD_ATTRIBUTE_16=getArray(R.array.CARD_ATTRIBUTE_16);
		CARD_LINK=getArray(R.array.CARD_LINK);
		CARD_LINK_16=getArray(R.array.CARD_LINK_16);
		
	}
	
	//效果类型
	public static String[] CARD_EFFECT_TYPE;
	//种族
	public static String[] CARD_RACE;
	//种族16进制
	public static String[] CARD_RACE_16;
	//等级
	public static String[] CARD_LEVELS;
	//等级10进制
	public static String[] CARD_LEVELS_16;
	//归属
	public static String[] CARD_OT;
	//归属16进制
	public static String[] CARD_OT_16;
	//卡片类型
	public static String[] CARD_TYPE;
	//卡片类型16进制
	public static String[] CARD_TYPE_16;
	//效果类型的16进制
	public static String[] CARD_EFFECT_TYPE_16;
	//属性
	public static String[] CARD_ATTRIBUTE;
	//属性16进制
	public static String[] CARD_ATTRIBUTE_16;
	
	public static String[] CARD_LINK;
	public static String[] CARD_LINK_16;
	
	public static final String[] CDB_TEXTS=new String[]{"id","name","desc","str1","str2","str3","str4","str5","str6","str7","str8","str9","str10","str11","str12","str13","str14","str15","str16"};
	public static final String[] CDB_DATAS=new String[]{"id","ot","type","level","attribute","race","atk","def","alias","category","setcode"};
	
	
	private static String[] getArray(int id){
		return OurygoApplication.getContext().getResources().getStringArray(id);	
	}
	
	/*获取卡片种族
	*race:数据库中卡片种族的二进制
	*/
	public static String getRace(String race){
		if(!race.equals("0")){
		//设置种族spinner默认选择
		String zz1=Integer.toHexString(Integer.parseInt(race));
		for (int i=0;i < CARD_RACE_16.length;i++)
		{
			if (zz1.equals(CARD_RACE_16[i]))
			{
				return CARD_RACE[i];
			}
		}
		}
		return "N/A";
	}
	
	/*获取卡片守备力
	 *cardType:数据库中卡片种类的二进制
	 *atk:数据库中卡片的def
	 */
	public static String getDef(int cardType, String def)
	{	
		if((cardType&1)!=0){
			//如果是怪兽
			return def;	
		}else{
			return "N/A";
		}
	}
	
	/*获取卡片攻击力
	*cardType:数据库中卡片种类的二进制
	*atk:数据库中卡片的atk
	*/
	public static String getAtk(int cardType, String atk)
	{
		if((cardType&1)!=0){
			//如果是怪兽
			return atk;	
		}else{
			return "N/A";
		}
	}
	
	/*获取卡片等级
	 *cardType:数据库中卡片种类的二进制
	 *atk:数据库中卡片的atk
	 */
	public static String getLevel(int cardType, String level)
	{
		if ((cardType & 1) ==1){
			//如果是怪兽
			if ((cardType & Integer.valueOf("4000000", 16)) != 0){

				//如果是链接怪兽
				/*String s=Integer.toBinaryString(Integer.parseInt(level));

				 int n=s.length()-s.replaceAll("1", "").length();
				 */
				return "N/A";
			}else{			
				return CardUtil.getLevel(Long.parseLong(level));
			}			
		}else{
			return "N/A";
		}
	}

	/*
	*获取卡片属性
	*attribute:数据库中的属性十六进制
	*/
	public static String getAttribute(String attribute16)
	{
		if(!attribute16.equals("0")){
		for (int i=0;i < CARD_ATTRIBUTE_16.length;i++){
			if (CARD_ATTRIBUTE_16[i].equals(attribute16)){
				return CARD_ATTRIBUTE[i];
			}
		}
		}
		return "N/A";
	}
	
	/*
	*获取等级16进制
	*level:等级
	*/
	public static String getLevel_16(String level) {
		if(level!=null&&!level.equals("")){
			for (int i=0;i < CARD_LEVELS.length;i++){
				if (CARD_LEVELS[i].equals(level)){
					return CARD_LEVELS_16[i];
				}
			}
		}
		return "0";
	}
	
	public static String getAttribute_16(String attribute)
	{
		if(attribute!=null&&!attribute.equals("")){
			for (int i=0;i < CARD_ATTRIBUTE.length;i++){
				if (CARD_ATTRIBUTE[i].equals(attribute)){
					return CARD_ATTRIBUTE_16[i];
				}
			}
		}
		return "0";
	}
	
	public static String getRace_16(String race)
	{
		if(race!=null&&!race.equals("")){
			for (int i=0;i < CARD_RACE.length;i++){
				if (CARD_RACE[i].indexOf(race)!=-1){
					return CARD_RACE_16[i];
				}
			}
		}
		return "0";
	}
	
	public static String getLink_16(String link)
	{
		StringBuffer link16=new StringBuffer("000000000");
		if(link!=null&&!link.equals("")){
			for (int i=0;i < CARD_LINK.length;i++){
				if (link.indexOf(CARD_LINK[i])!=-1){
					if(i<4){
					link16.replace(i,i+1,"1");
					}else{
					link16.replace(i+1,i+2,"1");
					}
				}
			}
		}
		return Integer.toHexString(Integer.parseInt(link16.toString()));
	}
	
	
	
	/*
	*获取卡片归属
	*ot:数据库中的卡片二进制
	*/
	public static String getOT(String ot){
		for(int i=0;i<CARD_OT_16.length;i++){
			if(CARD_OT_16[i].equals(ot)){
				return CARD_OT[i];
			}
		}
		return "未知归属";
	}
	
	/*
	 *获取卡片种类
	 *cardType:数据库中卡片种类的二进制
	 */
	public static List<String> getCardType(int cardType)
	{
		List<String> ls=new ArrayList<String>();
		for (int i=0;i < CARD_TYPE_16.length;i++)
		{
			int type=Integer.valueOf(CARD_TYPE_16[i],16);
			if ((cardType & type) == type)
			{
				ls.add(CARD_TYPE[i]);
			}
		}
		return ls;

	}
	
	/*
	 *获取效果类型
	 *effectType:数据库中效果种类的二进制
	 */
	public static List<String> getEffectType(int effectType)
	{
		List<String> ls=new ArrayList<String>();
		for (int i=0;i < CARD_EFFECT_TYPE_16.length;i++)
		{
			long type=Long.valueOf(CARD_EFFECT_TYPE_16[i],16);
			if ((effectType & type) == type)
			{
				ls.add(CARD_EFFECT_TYPE[i]);
			}
		}
		return ls;

	}
	

}
